package com.evan.webadmin.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.object.TokenData;
import com.evan.webadmin.biz.dto.EvluateRecordDto;
import com.evan.webadmin.biz.dto.EvluateDtoFilter;
import com.evan.webadmin.biz.dto.EvluateFilter;
import com.evan.webadmin.biz.dto.EvluateRecordItemDto;
import com.evan.webadmin.biz.model.Evluate;
import com.evan.webadmin.biz.dao.EvluateMapper;
import com.evan.webadmin.biz.service.IEvluateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Pavel
 * @since 2024-06-15
 */
@Service
public class EvluateServiceImpl extends ServiceImpl<EvluateMapper, Evluate> implements IEvluateService {

    @Autowired
    EvluateMapper evluateMapper;

    @Override
    public List<Evluate> findByCondition(EvluateFilter evluateFilter) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Evluate> record(EvluateRecordDto frontDto, HttpServletRequest request) {
        List<Evluate> evluates = new ArrayList<>();
        TokenData tokenData = (TokenData) request.getAttribute(TokenData.REQUEST_ATTRIBUTE_NAME);
        if(frontDto != null && CollectionUtils.isNotEmpty(frontDto.getRecords())){
            int index = 0;
            for(EvluateRecordItemDto item:frontDto.getRecords()){
                Evluate evluate = new Evluate();
                evluate.setModuleType(frontDto.getModuleType());
                evluate.setPersonalType(frontDto.getPersonnelType());
                evluate.setEvluateName(item.getTitle());
                evluate.setEvluateScore(item.getScore());
                evluate.setEvluateType(item.getType());
                evluate.setEvluateTxt(item.getTxt());
                evluate.setUserId(tokenData.getUserId());
                evluate.setUserName(tokenData.getShowName());
                evluate.setEvluateIndex(index++);
                evluate.setCreateTime(new Date());
                evluates.add(evluate);
            }
            saveBatch(evluates);
        }
        return ResponseResult.success(null);
    }

    @Override
    public ResponseResult<EvluateRecordDto> exist(EvluateDtoFilter filter, HttpServletRequest request) {
        TokenData tokenData = (TokenData) request.getAttribute(TokenData.REQUEST_ATTRIBUTE_NAME);
        QueryWrapper<Evluate> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", tokenData.getUserId());
        wrapper.orderByAsc("evluate_index");
        EvluateRecordDto record = new EvluateRecordDto();
        List<Evluate> evluates = evluateMapper.selectList(wrapper);
        if(CollectionUtils.isNotEmpty(evluates)){
            List<EvluateRecordItemDto> items = new ArrayList<>();
            record.setPersonnelType(evluates.get(0).getPersonalType());
            record.setModuleType(evluates.get(0).getModuleType());
            for(Evluate evluate:evluates){
                EvluateRecordItemDto item = new EvluateRecordItemDto();
                item.setTitle(evluate.getEvluateName());
                item.setScore(evluate.getEvluateScore());
                item.setType(evluate.getEvluateType());
                item.setTxt(evluate.getEvluateTxt());
                item.setIndex(evluate.getEvluateIndex());
                items.add(item);
            }
            record.setRecords(items);
        }
        return ResponseResult.success(record);
    }
}
